package zhizhemodi.tech.short_term_bwork.controller;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import zhizhemodi.tech.short_term_bwork.entity.Program;
import zhizhemodi.tech.short_term_bwork.service.ProgramService;
import zhizhemodi.tech.short_term_bwork.tool.CommonResult;

@RestController
@RequestMapping("program")
public class ProgramController {
    
    @Resource
    ProgramService programService;

    /**
     * 获取节目列表
     * @param dId
     * @param rowNum
     * @param pageNum
     * @return
     */
    @GetMapping("list")
    public CommonResult<?> getProgramList(
        @RequestParam("departmentId") Integer dId,
        @RequestParam(required = false, name = "limit") Integer rowNum,
        @RequestParam(required = false, name = "page") Integer pageNum
    ) {
        List<Program> programs = null;
        try {
            programs = programService.getProgramList(dId, rowNum, pageNum);
        } catch (Exception e) {
            return CommonResult.failed(e.getMessage());
        }
        return CommonResult.success(programs);
    }

    /**
     * 添加节目
     * @param program
     * @return
     */
    @PostMapping("add")
    public CommonResult<?> addProgram(
        @RequestBody Program program
    ) {
        try {
            programService.addProgram(program);
        } catch (Exception e) {
            return CommonResult.failed(e.getMessage());
        }
        return CommonResult.success("添加成功");
    }

    /**
     * 删除节目
     * @param programId
     * @return
     */
    @DeleteMapping("delete")
    public CommonResult<?> deleteProgram (
        @RequestParam("programId") Integer programId
    ) {
        try {
            programService.deleteProgram(programId);
        } catch (Exception e) {
            return CommonResult.failed(e.getMessage());
        }
        return CommonResult.success("删除成功");
    }
}
